// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    // 创建集合
    await db.createCollection('debts')
    await db.createCollection('debtors')

    // 创建索引
    await db.collection('debts').createIndex({
      name: 'creditor_index',
      unique: false,
      keys: {
        creditor: 1
      }
    })

    await db.collection('debtors').createIndex({
      name: 'card_number_index',
      unique: true,
      keys: {
        cardNumber: 1
      }
    })

    return {
      success: true,
      message: '初始化成功'
    }
  } catch (error) {
    return {
      success: false,
      message: error.message
    }
  }
}
